/*
 * TCSS 360 - Winter 2014
 * TESK Software
 */
package view.userProfile;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

import view.LogInView;
import view.MainFrame;

import view.Papers.CopyDocuments;

import java.awt.Color;

import javax.swing.ListSelectionModel;

import java.awt.BorderLayout;

import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.AbstractListModel;
import javax.swing.BoxLayout;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JScrollPane;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.SwingConstants;
import javax.swing.border.MatteBorder;
import javax.swing.UIManager;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

/**
 * Program Chair's profile.
 * @author Kon, Thang, Emily
 * @version March 20, 2014
 */
@SuppressWarnings("serial")
public class ProgramChairView extends JPanel{

	/** The frame the panel is associated with. */
	private final MainFrame my_frame;
	
	/** The default list model of the papers assign list. */
	private DefaultListModel assignListModel = new DefaultListModel();
	
	/** The default list model of the paper list for acceptance status. */
	private DefaultListModel paperListAcceptModel = new DefaultListModel();
	
	/** The default list mode for the paper list recommendation downloads. */
	private DefaultListModel paperListRecommendationModel = new DefaultListModel();
	
	/** The default list mode for the recommendations. */
	private DefaultListModel recommendations = new DefaultListModel();

	/** The list of papers that are available to be assigned. */
	private JList assignList;
	
	/** The list of papers for the decision list. */
	private JList paperDecisionList;
	
	/** The combo box that allows a user to choose accept or reject paper. */
	private JComboBox acceptRejectBox;
	
	/** A list of sub chairs that can be assigned papers. */
	private JList subChairList;

	/** The default list mode of paper decisions. */
	private DefaultListModel paperDecisionModel;

	/**
	 * Constructs a view for the program chair.
	 * 
	 * @param the_mainframe the frame to be associated with the panel
	 */
	public ProgramChairView(final MainFrame the_mainframe) {
		super();
		setBorder(new MatteBorder(2, 2, 2, 2, (Color) new Color(221, 160, 221)));
		my_frame = the_mainframe;
		init();
		this.setBackground(Color.white);

	}

	/**
	 * Adds specifications and components to the panel.
	 */
	@SuppressWarnings("unchecked")
	private void init(){

			setLayout(new FlowLayout(FlowLayout.CENTER, 5,5));
			JPanel welcomePanel = new JPanel();
			welcomePanel.setForeground(new Color(238, 130, 238));
			welcomePanel.setBorder(new MatteBorder(6, 0, 3, 0, (Color) new Color(186, 85, 211)));
			FlowLayout fl_welcomePanel = (FlowLayout) welcomePanel.getLayout();
			fl_welcomePanel.setVgap(20);
			add(welcomePanel);
			welcomePanel.setBackground(Color.white);
			JLabel lblNewLabel = new JLabel("Welcome, Program Chair!");
			lblNewLabel.setForeground(new Color(186, 85, 211));
			lblNewLabel.setBackground(new Color(255, 255, 255));
			lblNewLabel.setFont(new Font("Urdu Typesetting", Font.PLAIN, 18));
			
			
			
			ImageIcon icon = new ImageIcon(getClass().getResource("/girl.jpg"));
			final JLabel pic = new JLabel();
			pic.setIcon(icon);
			//pic.setIcon(new ImageIcon("..\\Conference System\\src\\girl.jpg"));
			welcomePanel.add(pic);
			welcomePanel.add(lblNewLabel);
			JPanel allTabbedPanel = new JPanel();
			allTabbedPanel.setPreferredSize(new Dimension(450,250));
			allTabbedPanel.setBackground(Color.white);
			add(allTabbedPanel);
			allTabbedPanel.setLayout(new BorderLayout(5, 5));
			
			JPanel buttonPanel = new JPanel();
			buttonPanel.setBackground(Color.WHITE);
			allTabbedPanel.add(buttonPanel, BorderLayout.SOUTH);
			//cancel and go back to previous panel, which is the profile.
			JButton cancelButton = new JButton("Log Out");
			cancelButton.setForeground(new Color(186, 85, 211));
			buttonPanel.add(cancelButton);
			cancelButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					JPanel view = new LogInView(my_frame);
					my_frame.setNewPanel(view);
				}
			});
			
			JPanel centerPanel = new JPanel();
			allTabbedPanel.add(centerPanel, BorderLayout.CENTER);
			centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.X_AXIS));
			JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
			tabbedPane.setBackground(new Color(248, 248, 255));
			tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
			tabbedPane.setFont(new Font("Toledo", Font.PLAIN, 14));
			centerPanel.add(tabbedPane);
			JPanel assignTab = new JPanel();
			assignTab.setBackground(Color.WHITE);
			tabbedPane.addTab("Assign Paper", null, assignTab, "Assign a paper to sub chair");
			
			GridBagLayout gbl_assignTab = new GridBagLayout();
			gbl_assignTab.columnWidths = new int[] {180, 150, 90};
			gbl_assignTab.rowHeights = new int[]{183, 0, 0};
			gbl_assignTab.rowWeights = new double[]{1.0, 1.0, Double.MIN_VALUE};
			assignTab.setLayout(gbl_assignTab);
			
			JPanel paperListPanel = new JPanel();
			paperListPanel.setBackground(Color.WHITE);
			GridBagConstraints gbc_paperListPanel = new GridBagConstraints();
			gbc_paperListPanel.fill = GridBagConstraints.BOTH;
			gbc_paperListPanel.insets = new Insets(0, 0, 5, 5);
			gbc_paperListPanel.gridx = 0;
			gbc_paperListPanel.gridy = 0;
			assignTab.add(paperListPanel, gbc_paperListPanel);
			
			GridBagLayout gbl_paperListPanel = new GridBagLayout();
			gbl_paperListPanel.columnWidths = new int[] {80};
			gbl_paperListPanel.rowHeights = new int[]{0, 126, 0};
			gbl_paperListPanel.columnWeights = new double[]{0.0};
			gbl_paperListPanel.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
			paperListPanel.setLayout(gbl_paperListPanel);
			
			JLabel lblTitle = new JLabel("Title");
			lblTitle.setBackground(Color.WHITE);
			lblTitle.setFont(new Font("Toledo", Font.ITALIC, 18));
			GridBagConstraints gbc_lblTitle = new GridBagConstraints();
			gbc_lblTitle.insets = new Insets(11, 0, 5, 5);
			gbc_lblTitle.anchor = GridBagConstraints.NORTH;
			gbc_lblTitle.gridx = 0;
			gbc_lblTitle.gridy = 0;
			paperListPanel.add(lblTitle, gbc_lblTitle);
		
		ArrayList<String> pcPapers = my_frame.db.getPapersAssignedForPC(my_frame.currentUser[0][0]);
		for (int i = 0; i < pcPapers.size(); i++) {
			assignListModel.addElement(pcPapers.get(i));
			
		}
		assignList = new JList(assignListModel);
		assignList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		assignList.setFont(new Font("Tahoma", Font.PLAIN, 15));
		
			GridBagConstraints gbc_list = new GridBagConstraints();
			gbc_list.anchor = GridBagConstraints.NORTHWEST;
			gbc_list.gridx = 0;
			gbc_list.gridy = 1;
			paperListPanel.add(assignList, gbc_list);
			JPanel subChairPanel = new JPanel();
			subChairPanel.setBackground(Color.WHITE);
			GridBagConstraints gbc_subChairPanel = new GridBagConstraints();
			gbc_subChairPanel.anchor = GridBagConstraints.WEST;
			gbc_subChairPanel.fill = GridBagConstraints.VERTICAL;
			gbc_subChairPanel.insets = new Insets(5, 0, 5, 5);
			gbc_subChairPanel.gridx = 1;
			gbc_subChairPanel.gridy = 0;
			assignTab.add(subChairPanel, gbc_subChairPanel);
			GridBagLayout gbl_subChairPanel = new GridBagLayout();
			gbl_subChairPanel.columnWidths = new int[] {140};
			gbl_subChairPanel.rowHeights = new int[]{0, 126, 0};
			gbl_subChairPanel.columnWeights = new double[]{0.0};
			gbl_subChairPanel.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
			subChairPanel.setLayout(gbl_subChairPanel);
			JLabel lblSubprogramChair = new JLabel("Sub Chair");
			lblSubprogramChair.setHorizontalAlignment(SwingConstants.CENTER);
			lblSubprogramChair.setBackground(Color.WHITE);
			lblSubprogramChair.setFont(new Font("Toledo", Font.ITALIC, 18));
			GridBagConstraints gbc_lblSubprogramChair = new GridBagConstraints();
			gbc_lblSubprogramChair.fill = GridBagConstraints.VERTICAL;
			gbc_lblSubprogramChair.insets = new Insets(5, 0, 5, 5);
			gbc_lblSubprogramChair.gridx = 0;
			gbc_lblSubprogramChair.gridy = 0;
			subChairPanel.add(lblSubprogramChair, gbc_lblSubprogramChair);
			
			//*************************************************** LIST OF SUB CHAIR ****************************
			ArrayList<String> subChairs = my_frame.db.getSubChairs(my_frame.db.getConference(my_frame.currentUser[0][0]));
			final String[] subChairArray = new String[subChairs.size()];
			for (int i = 0; i < subChairs.size(); i++) {
				subChairArray[i] = subChairs.get(i);
			}
			subChairList = new JList();
			subChairList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			subChairList.setFont(new Font("Tahoma", Font.PLAIN, 15));
			subChairList.setModel(new AbstractListModel() {
				String[] values = subChairArray;
				public int getSize() {
					return values.length;
				}
				public Object getElementAt(int index) {
					return values[index];
				}
			});
		
		GridBagConstraints gbc_subChairList = new GridBagConstraints();
		gbc_subChairList.insets = new Insets(0, 0, 0, 5);
		gbc_subChairList.anchor = GridBagConstraints.NORTHWEST;
		gbc_subChairList.gridx = 0;
		gbc_subChairList.gridy = 1;
		subChairPanel.add(subChairList, gbc_subChairList);
		JPanel commentPanel = new JPanel();
		commentPanel.setBackground(Color.WHITE);
		FlowLayout flowLayout = (FlowLayout) commentPanel.getLayout();
		flowLayout.setVgap(50);
		GridBagConstraints gbc_commentPanel = new GridBagConstraints();
		gbc_commentPanel.insets = new Insets(0, 0, 5, 0);
		gbc_commentPanel.fill = GridBagConstraints.BOTH;
		gbc_commentPanel.gridx = 2;
		gbc_commentPanel.gridy = 0;
		assignTab.add(commentPanel, gbc_commentPanel);
		//*******************************************************Assign paper to sub chair BUTTON ************
		JButton assignButton = new JButton("Assign");
		assignButton.setBackground(UIManager.getColor("Button.background"));
		assignButton.setForeground(new Color(218, 112, 214));
		assignButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(!assignList.isSelectionEmpty()){
					String subChair = subChairList.getSelectedValue().toString();
					String pc = my_frame.currentUser[0][0];
					String conference = my_frame.db.getConference(pc);
					String paper = assignList.getSelectedValue().toString();
					String author = my_frame.db.getAuthor(paper, conference);
					ArrayList<String> subChairPapers = my_frame.db.getPapersAssignedForSC(subChair);
					int count = 0;
					for (int i = 0; i < subChairPapers.size(); i++) {
						if (subChairPapers.get(i) != "") {
							count++;
						}
					}
					if (count > 3) {
						JOptionPane.showMessageDialog(my_frame, "SubChair is already assigned 4 papers!", "Error!", JOptionPane.ERROR_MESSAGE);
					} else {
						my_frame.db.assignSubChairToPaper(subChair, conference, author, paper);
						JOptionPane.showMessageDialog(my_frame, "Sucessfully Assigned Paper!", "Sucess!", JOptionPane.PLAIN_MESSAGE);
					}
				} else {
					JOptionPane.showMessageDialog(null, "Select a Paper");
				}
			}
		});
		assignButton.setToolTipText("Click here to assign a paper to a sub program chair.");
		assignButton.setFont(new Font("Toledo", Font.BOLD | Font.ITALIC, 14));
		commentPanel.add(assignButton);
		JPanel decisionTab = new JPanel();
		decisionTab.setBackground(new Color(221, 160, 221));
		tabbedPane.addTab("Decision", null, decisionTab, "Accept or Reject a paper by click on the radio button");
		decisionTab.setPreferredSize(new Dimension(500,310));
		final ButtonGroup group = new ButtonGroup();
		JRadioButton buttons;
		decisionTab.setLayout(null);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(0, 0, 295, 152);
		decisionTab.add(scrollPane);
		//**********************************************************************DECISION TAB*************
		// The Paper List.
		paperDecisionModel = new DefaultListModel();
		
		final String[] decisionArray = new String[pcPapers.size()];
		for (int i = 0; i < pcPapers.size(); i++) {
			String pc = my_frame.currentUser[0][0];
			String paperName = pcPapers.get(i);
			String conference = my_frame.db.getConference(pc);
				if (paperName != "") {
					String authorName = my_frame.db.getAuthor(paperName, conference);
					int decision = my_frame.db.getAcceptanceStatus(pc, paperName, authorName);
					if (decision == -1) {
						decisionArray[i] = "Undecided";
					} else if (decision == 0) {
						decisionArray[i] = "Rejected";
					} else if (decision == 1) {
						decisionArray[i] = "Accepted";
					} else {
						decisionArray[i] = null;
					}
					paperDecisionModel.addElement(pcPapers.get(i) +"-"+ decisionArray[i]);
					paperListRecommendationModel.addElement(pcPapers.get(i));
				}
		}
		

		paperDecisionList = new JList(paperDecisionModel);

		scrollPane.setRowHeaderView(paperDecisionList);
		paperDecisionList.setFont(new Font("Tahoma", Font.PLAIN, 18));
		paperDecisionList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		acceptRejectBox = new JComboBox();
		acceptRejectBox.setToolTipText("Click Accept or Reject to make a decision");
		acceptRejectBox.setFont(new Font("Tahoma", Font.PLAIN, 12));
		acceptRejectBox.setModel(new DefaultComboBoxModel(new String[] {"--Decision--", "Accept", "Reject"}));
		acceptRejectBox.setBounds(318, 17, 104, 27);
		decisionTab.add(acceptRejectBox);
		
		JButton btnSubmit = new JButton("Submit");
		btnSubmit.setBounds(318, 75, 104, 29);
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(paperDecisionList.isSelectionEmpty()) {
					JOptionPane.showMessageDialog(my_frame, "Please select a Paper", "Warning", JOptionPane.WARNING_MESSAGE);
				}
				else if (acceptRejectBox.getSelectedIndex() < 1){
					JOptionPane.showMessageDialog(my_frame, "You have to make a decision of Accept or Reject before submitting.", "Warning", JOptionPane.WARNING_MESSAGE);

					
				}
				else {
				    String[] paperDecision = paperDecisionList.getSelectedValue().toString().split("-");
					String decision = acceptRejectBox.getSelectedItem().toString();
					String paper = paperDecision[0];
					String pc = my_frame.currentUser[0][0];
					String conference = my_frame.db.getConference(pc);
					String author = my_frame.db.getAuthor(paper, conference);
					if (decision.equals("Accept")) {
						my_frame.db.addAcceptanceStatus(pc, paper, author, 1);	
						paperDecisionModel.remove(paperDecisionList.getSelectedIndex());
						paperDecisionModel.addElement(paper+"-Accepted");
						
						JOptionPane.showMessageDialog(my_frame, "Change Successfully Made!", "Sucess!", JOptionPane.PLAIN_MESSAGE);
					} else if (decision.equals("Reject")) {
						my_frame.db.addAcceptanceStatus(pc, paper, author, 0);
						paperDecisionModel.remove(paperDecisionList.getSelectedIndex());
						paperDecisionModel.addElement(paper+"-Rejected");
						JOptionPane.showMessageDialog(my_frame, "Change Successfully Made!", "Sucess!", JOptionPane.PLAIN_MESSAGE);
					}	
				}
			}
		});
		decisionTab.add(btnSubmit);

	}
}
